package com.kpx.app.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.widget.RadioGroup;

import com.kpx.app.Application;
import com.kpx.app.R;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.bean.LoginUser;
import com.kpx.app.bean.MenuBean;
import com.kpx.app.configs.BroadcastFilters;
import com.kpx.app.configs.Constants;
import com.kpx.app.fragment.FragmentMine;
import com.kpx.app.fragment.FragmentMyTask;
import com.kpx.app.thirdparty.JPush.JPushUtils;
import com.kpx.app.utils.StringUtil;
import com.kpx.app.utils.UIUtils;
import com.kpx.app.utils.UserInfoUtils;
import com.kpx.app.utils.datautils.FileUtils;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.wigdet.DialogFactoryImpl;

import cn.jpush.android.api.JPushInterface;

import static com.kpx.app.utils.UserInfoUtils.getCachedLoginUser;
import static com.kpx.app.utils.UserInfoUtils.isUserLogin;

/**
 * Created by Administrator on 2016/12/19.
 */
public class ActivityMain extends ActivityBase {

    private final Fragment[] mFragments = new Fragment[4];// 初始化fragment的管理类
    private final FragmentManager fragmentManager = getSupportFragmentManager();
    private RadioGroup group_tab;
    private int mCheckedId;
    private int checkedNum;
    private FragmentTransaction fragmentTransaction;

    @Override
    public void initview(@Nullable Bundle savedInstanceState) {

    }

    @Override
    public void initData() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        group_tab = (RadioGroup) findViewById(R.id.group_tab);
        setClickListener();//设置点击事件监听
        checkedNum = 0;
        mFragments[0] = fragmentManager.findFragmentById(R.id.fragment_home);// 首页
        mFragments[1] = fragmentManager.findFragmentById(R.id.fragment_task);// 任务
        mFragments[2] = fragmentManager.findFragmentById(R.id.fragment_growth);// 成长
        mFragments[3] = fragmentManager.findFragmentById(R.id.fragment_mine);// 我的
        group_tab.check(R.id.radio_home);
        fragmentTransaction = fragmentManager.beginTransaction().hide(mFragments[0]).hide(mFragments[1]).hide(mFragments[2]).hide(mFragments[3]);
        fragmentTransaction.show(mFragments[checkedNum]).commit();// 默认显示主页面
        mCheckedId = R.id.radio_home;// 默认选中第一个
    }

    public void onReceiver(Context context, Intent intent) {
        if (intent.getAction().equals(BroadcastFilters.BROADCAST_ACTION_LOGIN_SUCCESS_BACK)) {
            FragmentMyTask fragmentMyTask = ((FragmentMyTask) fragmentManager.findFragmentById(R.id.fragment_task));
            if (fragmentMyTask != null)
                fragmentMyTask.refreshData();
            FragmentMine fragmentMine = ((FragmentMine) fragmentManager.findFragmentById(R.id.fragment_mine));
            if (fragmentMine != null)
                fragmentMine.loadData();
        } else if (intent.getAction().equals(BroadcastFilters.BROADCAST_ACTION_JPUSH)) {
            if (TextUtils.isEmpty(intent.getStringExtra("jpush"))) {
                return;
            }
            ToastUtil.getInstance().showDialog(this, intent.getStringExtra("jpush"));
        } else if (intent.getAction().equals(BroadcastFilters.BROADCAST_ACTION_REFRESH_TASK_DATA)) {
            FragmentMyTask fragmentMyTask = ((FragmentMyTask) fragmentManager.findFragmentById(R.id.fragment_task));
            if (fragmentMyTask != null)
                fragmentMyTask.refreshData();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (isUserLogin()) {
            JPushUtils.getInstance().setAlias(getCachedLoginUser().getTOKEN());
            JPushInterface.setDebugMode(Constants.DEBUG);// 设置开启日志,发布时请关闭日志
            JPushInterface.init(this); // 初始化 JPush
        }
    }

    /**
     * 点击事件设置
     */
    private void setClickListener() {
        //超市导航栏选择跳转
        group_tab.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                                                 @Override
                                                 public void onCheckedChanged(RadioGroup group, int checkedId) {
                                                     fragmentTransaction = fragmentManager.beginTransaction().hide(mFragments[0]).hide(mFragments[1]).hide(mFragments[2])
                                                             .hide(mFragments[3]);
                                                     switch (checkedId) {
                                                         case R.id.radio_home:// 主页
                                                             checkedNum = 0;
                                                             mCheckedId = R.id.radio_home;
                                                             break;
                                                         case R.id.radio_task:// 任务
                                                             checkedNum = 1;
                                                             mCheckedId = R.id.radio_task;
                                                             break;
                                                         case R.id.radio_growth:// 帮助
                                                             checkedNum = 2;
                                                             mCheckedId = R.id.radio_growth;
                                                             break;
                                                         case R.id.radio_mine:// 我的
                                                             if (!isUserLogin()) {
                                                                 UserInfoUtils.showLoginDialog(ActivityMain.this, "请先登录", 0);
                                                                 group_tab.check(mCheckedId);
                                                                 return;
                                                             }
                                                             checkedNum = 3;
                                                             mCheckedId = R.id.radio_mine;
                                                             FragmentMine fragmentMine = ((FragmentMine) fragmentManager.findFragmentById(R.id.fragment_mine));
                                                             if (fragmentMine != null)
                                                                 fragmentMine.refreshData();
                                                             break;
                                                     }
                                                     fragmentTransaction.show(mFragments[checkedNum]).commitAllowingStateLoss();// 显示指定的页面
                                                 }
                                             }
        );
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    public void selectTab(int i) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if (isFinishing() || isDestroyed()) {
                return;
            }
        } else {
            if (isFinishing()) {
                return;
            }
        }
        switch (i) {
            case 0:
                mCheckedId = R.id.radio_home;
                break;
            case 1:
                mCheckedId = R.id.radio_task;
                break;
            case 2:
                mCheckedId = R.id.radio_growth;
                break;
            case 3:
                mCheckedId = R.id.radio_mine;
                break;
        }
        group_tab.check(mCheckedId);
    }

    public void changeTab(MenuBean b) {
        selectTab(1);
        FragmentMyTask task = (FragmentMyTask) fragmentManager.findFragmentById(R.id.fragment_task);
        task.refreshData(b);
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Application.instance == null) {
            return;
        }
        if (intent.resolveActivity(Application.instance.getPackageManager()) != null) {
            Application.instance.getApplicationContext().startActivity(intent);
        }
    }

    public void changeToHelp() {
        selectTab(2);
    }
}